<?php
namespace app\modules\backend\controllers;

use app\models\Area;
use app\models\Base;
use app\tools\Re;
use Yii;

class AreaController extends BaseController
{
    //城市接口(二级)
    public function actionCityList(){
        $city_data = [];
        $citys_one = Area::find()->where(['parent_id' => 0])->orderBy("area_code asc")->all();
        if (!empty($citys_one)) {
            foreach ($citys_one as $v1) {
                $city_two_data = [];
                $citys_two = Area::find()->where(['parent_id' => $v1->id, 'level' => 1])->orderBy("area_code asc")->all();
                if (!empty($citys_two)) {
                    foreach ($citys_two as $v2) {
                        $city_two_data[] = [
                            'value' => (int)$v2->id,
                            'label' => $v2->short_name,
                        ];
                    }
                }
                $city_data[] = [
                    'value' => (int)$v1->id,
                    'label' => $v1->short_name,
                    'children' => $city_two_data
                ];
            }
        }
        return Re::data(['data' => $city_data]);
    }

    //城市接口(三级)
    public function actionThreeCityList(){
        $type = Yii::$app->request->post('type', 0);
        $city_data = [];
        $citys_one = Area::find()->where(['parent_id' => 0])->orderBy("area_code asc")->all();
        if (!empty($citys_one)) {
            foreach ($citys_one as $v1) {
                $city_two_data = [];
                $citys_two = Area::find()->where(['parent_id' => $v1->id, 'level' => 1])->orderBy("area_code asc")->all();
                if (!empty($citys_two)) {
                    foreach ($citys_two as $v2) {
                        $city_three_data = [];
                        $citys_three = Area::find()->where(['parent_id' => $v2->id, 'level' => 2])->andWhere("short_name <> '$v2->short_name'")->orderBy("area_code asc")->all();
                        if (!empty($citys_three)) {
                            foreach ($citys_three as $v3) {
                                $city_three_data[] = [
                                    'value' => (int)$v3->id,
                                    'label' => $v3->short_name,
                                ];
                            }
                        }
                        $city_two_data[] = [
                            'value' => (int)$v2->id,
                            'label' => $v2->short_name,
                            'children' => $city_three_data
                        ];
                    }
                }
                $city_data[] = [
                    'value' => (int)$v1->id,
                    'label' => $v1->short_name,
                    'children' => $city_two_data
                ];
            }
        }
        return Re::data(['data' => $city_data]);
    }

    //城市接口
    public function actionAllAreaList(){
        $area_id = Yii::$app->request->post('area_id', '');
        $area_arr = $area_id ? explode(',', $area_id) : [];
        $all_area_data = [];
        $province_list = Area::find()->where(['parent_id' => 0])->orderBy("area_code asc")->all();
        if (!empty($province_list)) {
            foreach ($province_list as $p) {
                $city_data = [];
                $city_list = Area::find()->where(['parent_id' => $p->id, 'level' => 1])->orderBy("area_code asc")->all();
                if (!empty($city_list)) {
                    foreach ($city_list as $c) {
                        $area_data = [];
                        $area_list = Area::find()->where(['parent_id' => $c->id, 'level' => 2])->andWhere("short_name <> '$c->short_name'")->orderBy("area_code asc")->all();
                        if (!empty($area_list)) {
                            foreach ($area_list as $a) {
                                $area_data[] = [
                                    'id' => $a->id,
                                    'title' => $a->name,
                                    'checked' => in_array($a->id, $area_arr)
                                ];
                            }
                        }
                        $city_data[] = [
                            'id' => $a->id,
                            'title' => $c->name,
                            'children' => $area_data
                        ];
                    }
                }
                $all_area_data[] = [
                    'id' => $a->id,
                    'title' => $p->name,
                    'children' => $city_data
                ];
            }
        }
        return Re::data(['data' => $all_area_data]);
    }

    //城市接口(二级)
    public function actionGetAreaInfo(){
        $id = Yii::$app->request->post('id', 0);
        $area_info = Area::findOne($id);
        if (empty($id) || empty($area_info)) {
            return Re::data(null, '信息读取失败', Re::ERROR);
        }
        return Re::data(['data' => $area_info]);
    }

    public function actionGetLngLat() {
        $city = Yii::$app->request->post('city', 0);
        $city_info = Area::findOne($city);
        if (empty($city_info)) {
            return Re::data(null, '信息读取失败', Re::ERROR);
        }
        $method = "GET";
        $url = 'http://restapi.amap.com/v3/geocode/geo?key=d91c1a89a746f4da1d732a3364a7d522&address='.$city_info->short_name;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        $result_data = json_decode(curl_exec($curl));
        $location = $result_data->geocodes[0]->location;
        $lat = '0.00';
        $lng = '0.00';
        if (!empty($location)) {
            $location_arr = explode(',', $location);
            $lng = $location_arr[0];
            $lat = $location_arr[1];
        }
        $data = [
            'lng' => $lng,
            'lat' => $lat
        ];
        return Re::data(['data' => $data]);
    }
}
